package com.thirtywords.analizer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import com.thirtywords.analizer.beans.Code;

public class Downloader {

	public static Code getCode(String url) {

		try {

			Code code;

			if (!Analyzer.cache.containsKey(url)) {

				code = new Code(url);
				URL myUrl = new URL(code.getUrl());
				BufferedReader in = new BufferedReader(new InputStreamReader(
						myUrl.openStream(), "UTF-8"));

				String line = "";
				String html = "";

				while ((line = in.readLine()) != null)
					html += line;

				in.close();

				code.setOriginalCode(html);

				return code;
			} else {
				return Analyzer.cache.get(url);
			}
		}catch(IOException e) {
			//Do nothing
		}catch(IllegalArgumentException e) {
			//Do nothing
		}catch(Exception e) {
			e.printStackTrace();
		}

		return null;
	}
}
